<?php

use yii\helpers\Html;
use yii\grid\GridView;
use backend\models\Contract;
use backend\models\Contractor;
use backend\helpers\ActionButton;
use backend\models\ContractStatus; 
use backend\helpers\FileViewHelper;
use kartik\select2\Select2;
use kartik\export\ExportMenu;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\ContractSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->registerJs(
    "$('.js-file-exist').tooltip();",
);

$this->title = 'Договоры';
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="container contract-index">

    <h1 class="main-title"><?= Html::encode($this->title) ?></h1>

    <div class="action-menu action-menu__container">
        <div>
            <?= ActionButton::create('Добавить договор', 'createContract') ?>
            <a class="btn btn-primary" href="#" data-toggle="collapse" data-target="#js-collapse-search" aria-expanded="false" aria-controls="collapseExample">Поиск</a>
        </div>
        <div>
            <?= ActionButton::dictionaryLink('Статусы договоров', 'contract-status/index', 'browseContractStatus');?>
            <?= ActionButton::dictionaryLink('Цели', 'contract-purpose/index', 'browseContractPurpose');?>
        </div>
    </div>

    <?php echo $this->render('_search', ['model' => $searchModel]); ?>

</div>


<div class="container-fluid contract-table-container">
    
    <?
        $gridColumns = [
            'own_number',
            [
                'attribute' => 'id_contractor',
                'value' => 'contractor.name_short_opf',
            ],
            'subject',
            'total_price',
            [
                'attribute' => 'date_sign',
                'format' => 'date',
            ],
            [
                'attribute' => 'doc_kp_answer_one',
                'value' => function ($model) { return FileViewHelper::urlOnly(Contract::BUCKET_NAME, $model->doc_kp_answer_one); },
                'format' => 'raw'
            ],
            [
                'attribute' => 'doc_kp_answer_two',
                'value' => function ($model) { return FileViewHelper::urlOnly(Contract::BUCKET_NAME, $model->doc_kp_answer_two); },
                'format' => 'raw'
            ],
            [
                'attribute' => 'doc_kp_screenshot',
                'value' => function ($model) { return FileViewHelper::urlOnly(Contract::BUCKET_NAME, $model->doc_kp_screenshot); },
                'format' => 'raw'
            ],
            [
                'attribute'=>'doc_main_contract',
                'value' => function ($model) { return FileViewHelper::urlOnly(Contract::BUCKET_NAME, $model->doc_main_contract); },
                'format' => 'raw'
            ],
            [
                'attribute'=>'doc_invoice',
                'value' => function ($model) { return FileViewHelper::urlOnly(Contract::BUCKET_NAME, $model->doc_invoice); },
                'format' => 'raw'
            ],
            [
                'attribute'=>'doc_close',
                'value' => function ($model) { return FileViewHelper::urlOnly(Contract::BUCKET_NAME, $model->doc_close); },
                'format' => 'raw'
            ],
            [
                'attribute'=>'doc_payment_order',
                'value' => function ($model) { return FileViewHelper::urlOnly(Contract::BUCKET_NAME, $model->doc_payment_order); },
                'format' => 'raw'
            ],
        ];
        echo ExportMenu::widget([
            'dataProvider' => $dataProvider,
            'columns' => $gridColumns,
            'exportConfig' => [
                ExportMenu::FORMAT_HTML => false,
                ExportMenu::FORMAT_CSV => false,
                ExportMenu::FORMAT_TEXT => false,
                ExportMenu::FORMAT_EXCEL => false,
            ]
        ]);
    ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'showFooter' => true,
        'tableOptions' => [
            'class' => 'table table-striped'
        ],
        'pager' => [
            'firstPageLabel' => '|«',
            'lastPageLabel'  => '»|'
        ],
        'columns' => [
            [
                'attribute' => 'own_number',
                'format'=>'raw',
                'footer'=> 'Итого: ' . $dataProvider->getTotalCount(),
            ],
            [
                'attribute' => 'id_contractor',
                'filter' => Select2::widget([
                    'name' => 'contractor_in_grid',
                    'model' => $searchModel,
                    'data' => Contractor::contractorList(),
                    'attribute' => 'id_contractor',
                    'value' => 'contractor.name_short_opf',
                    'options' => [
                        'id' => 'select2-contractor__grid',
                        'class' => 'form-control form-control-sm',
                        'placeholder' => 'Выберите контрагента...'
                    ],
                    'pluginOptions' => [
                        'allowClear' => true
                    ],
                ]),
                'value' => 'contractor.name_short_opf',
            ],
            'subject',
            [
                'attribute' => 'total_price',
                'format' => 'currency',
                'footer'=> 'Итого: ' . Yii::$app->formatter->asCurrency($dataProvider->query->sum('total_price')),
            ],
            [
                'attribute' => 'date_sign',
                'filter' => Html::activeInput('date', $searchModel, 'date_sign', ['class' => 'form-control']),
                'format' => 'date',
            ],
            [
                'attribute' => 'date_realize',
                'filter' => Html::activeInput('date', $searchModel, 'date_realize', ['class' => 'form-control']),
                'format' => 'date',
            ],
            [
                'attribute' => 'id_status',
                'filter' => ContractStatus::find()->select(['title', 'id'])->indexBy('id')->column(),
                'filterInputOptions' => ['class' => 'form-control form-control-sm'],
                'value' => function($model) { 
                    return '<span class="badge ' . $model->status->css_style . '">' . $model->status->title . '</span>'; 
                },
                'format' => 'raw',
            ],
            [
                'label' => 'Способ закупки',
                'attribute' => 'purchaseMethod.title',
            ],
            [
                'label' => 'Файл',
                'value' => function ($model) { return FileViewHelper::simpleWithIcon(\backend\models\Contract::BUCKET_NAME, $model->doc_main_contract); },
                'format' => 'raw',
            ],
            [
                'label' => 'Файлы',
                'value' => function ($model) { return FileViewHelper::fileExist($model); },
                'format' => 'raw',
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'visibleButtons' =>
                [
                    'view' => Yii::$app->user->can('browseContract'),
                    'update' => Yii::$app->user->can('updateContract'),
                    'delete' => Yii::$app->user->can('deleteContract'),
                ]
            ],
        ],
    ]); ?>

</div>